package io.coderyeah.search.controller;

import io.coderyeah.basic.Result;
import io.coderyeah.basic.exception.BusinessException;
import io.coderyeah.search.domain.SearchMasterMsg;
import io.coderyeah.search.dto.SearchDTO;
import io.coderyeah.search.service.SearchMasterMsgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author lqs
 * @date 2022/9/28 10:46
 */
@Api("寻主消息接口")
@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    private SearchMasterMsgService searchMasterMsgService;

    @ApiOperation("发布寻主消息")
    @PostMapping("/publish")
    public Result publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest req) {
        return searchMasterMsgService.publish(searchMasterMsg, req);
    }

    /**
     * 用户查询自己所有的寻主消息
     *
     * @return result
     */
    @ApiOperation("用户查询自己所有的寻主消息")
    @PostMapping("/user")
    public Result getAllMsgByUserId(@RequestBody SearchDTO searchDTO, HttpServletRequest req) {
        return searchMasterMsgService.getAllMsgByUserId(searchDTO, req);
    }

    /**
     * 管理员查询待处理消息
     *
     * @param searchDTO
     * @param req
     * @return
     */
    @ApiOperation("管理员查询待处理消息")
    @PostMapping("/toHandle")
    public Result getMsg2Handle(@RequestBody SearchDTO searchDTO, HttpServletRequest req) {
        return searchMasterMsgService.toHandle(searchDTO, req);
    }

    @ApiOperation("管理员查询寻主池消息")
    @PostMapping("/pool")
    public Result getMsgByPool(@RequestBody SearchDTO searchDTO, HttpServletRequest req) {
        return searchMasterMsgService.pool(searchDTO, req);
    }


    @ApiOperation("管理员查询处理完成消息")
    @PostMapping("/finish")
    public Result getFinishedMsg(@RequestBody SearchDTO searchDTO, HttpServletRequest req) {
        return searchMasterMsgService.getFinishedMsg(searchDTO, req);
    }

    @ApiOperation("拒单")
    @PostMapping("/reject/{id}")
    public Result reject(@PathVariable("id") Long id) {
        return searchMasterMsgService.reject(id);
    }

    @ApiOperation("接单")
    @PostMapping("/accept")
    public Result accept(@RequestBody Map<String, String> map) {
        try {
            return searchMasterMsgService.accept(map);
        } catch (BusinessException e) {
            return Result.fail(e.getMessage());
        }
    }
}
